package model

import (
	"log"
	"poker/utils"
	"sync"
)

var (
	rankCacheInstance *RankCache
	globalAccessToken sync.Map
)

func Init() {
	rankCacheInstance = &RankCache{
		cache: make(map[string][]VoteDate),
	}
	rankCacheInstance.LoadFromDatabase()

	keys, err := utils.LoadKeys("./conf/keys.txt")
	if err != nil {
		log.Println("读取 keys.txt 文件失败:", err)
	}
	for _, v := range keys {
		go refreshToken(v[0], v[2])
	}
}

// GetRankCache 获取 RankCache 的单例实例
func GetRankCache() *RankCache {
	return rankCacheInstance
}

func GetAccessToken(appid string) string {
	if token, ok := globalAccessToken.Load(appid); ok {
		return token.(string)
	}
	return ""
}
